﻿using DryIoc;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLineNew.Models.Entity.BaseInfo;
using ZoneLineNew.Services.DB;
using ZoneLineNew.Services.Message;
using ZoneLineNew.ViewModelBase;
using ZoneLineNew.Views.Dialogs;

namespace ZoneLineNew.ViewModels {
    
    public class CustomerViewModel : NavigationViewModel {

        public CustomerViewModel(
            IRegionManager regionManager,
            IDialogService dialog,
            IMessageService messageBox,
            IEventAggregator eventAggregator,
            IDbOperate<IFreeSqlMainContext> mainDbOperate
            ) : base(regionManager, dialog, messageBox, eventAggregator, mainDbOperate) {

        }

        private string searchText;

        public string SearchText {
            get { return searchText; }
            set { searchText = value; base.RaisePropertyChanged(); }
        }

        private ObservableCollection<CustomerEntity> customers = new ObservableCollection<CustomerEntity>();

        public ObservableCollection<CustomerEntity> Customers {
            get { return customers; }
            set { customers = value; base.RaisePropertyChanged(); }
        }

        public DelegateCommand SearchCommand => new DelegateCommand(() => {
            LoadData();
        });

        public DelegateCommand AddCommand => new DelegateCommand(() => {
            DialogParameters keyValuePairs = new DialogParameters();
            keyValuePairs.Add("Title", "新增客户");
            _dialog.ShowDialog(nameof(EditCustomerDialogView), keyValuePairs, (rhs) => {
                if (rhs.Result == ButtonResult.OK) {
                    LoadData();
                }
            });
        });

        public DelegateCommand<CustomerEntity> EditCommand => new DelegateCommand<CustomerEntity>((arg) => {
            if (arg != null) {
                DialogParameters keyValuePairs = new DialogParameters();
                keyValuePairs.Add("Title", "编辑客户");
                keyValuePairs.Add("CustomerEntity", JsonConvert.SerializeObject(arg));
                _dialog.ShowDialog(nameof(EditCustomerDialogView), keyValuePairs, (rhs) => {
                    if (rhs.Result == ButtonResult.OK) {
                        LoadData();
                    }
                });
            }
        });

        public DelegateCommand<CustomerEntity> DeleteCommand => new DelegateCommand<CustomerEntity>((arg) => {
            if (arg != null) {
                var dialogAsk = _message.Ask("确定是否删除此数据？");
                if (dialogAsk == System.Windows.MessageBoxResult.OK) {
                    var res = _mainDbOperate.DeleteModel<CustomerEntity>(o=>o.Id == arg.Id);
                    if (res) {
                        LoadData();
                    }
                }
            }
        });

        protected override void OnViewLoaded() {
            LoadData();
            //base.OnViewLoaded();
        }

        private void LoadData() {
            var list = _mainDbOperate.GetFreeSql().Queryable<CustomerEntity>()
                .WhereIf(!string.IsNullOrEmpty(SearchText), o => o.Name.Contains(SearchText))
                .ToList();
            Customers.Clear();
            Customers.AddRange(list);
        }
    }

}
